package com.xxx.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.article.domain.Article;
import com.xxx.article.service.ArticleService;
import com.xxx.entity.PageResult;
import com.xxx.entity.Result;
import com.xxx.entity.StatusCode;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * {@link CrossOrigin}开启跨域请求
 *
 * @author blijojo
 * @date 2021/6/11
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    private final ArticleService articleService;

    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 头条文章
     */
    @GetMapping("/top")
    public Result top() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("istop", "1");
        List<Article> articleList = articleService.list(wrapper);
        return new Result(StatusCode.OK, true, "查询头条文章成功", articleList);
    }

    /**
     * 文章审核
     */
    @PutMapping("/examine/{articleId}")
    public Result examine(@PathVariable("articleId") String articleId) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", articleId).set("state", "1");
        articleService.update(wrapper);
        return new Result(StatusCode.OK, true, "操作成功", null);
    }

    /**
     * 根据专栏ID获取文章列表
     */
    @PostMapping("/column/{columnId}/{page}/{size}")
    public Result searchByColumnId(@PathVariable("columnId") String columnId,
                                   @PathVariable("page") Integer current,
                                   @PathVariable("size") Integer size) {
        Page<Article> pageData = new Page<>(current, size);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("columnid", columnId);
        return getResultWithPage(pageData, wrapper);
    }

    /**
     * 查询分页数据,封装到PageResult对象,再封装到Result返回
     *
     * @param pageData 简单分页模型 封装当前页码和每页记录数
     * @param wrapper  查询条件包装类
     * @return Result
     */
    private Result getResultWithPage(Page<Article> pageData, QueryWrapper<Article> wrapper) {
        Page<Article> articlePage = articleService.page(pageData, wrapper);
        PageResult<Article> pageResult = new PageResult<>();
        pageResult.setTotal(articlePage.getTotal());
        pageResult.setRows(articlePage.getRecords());
        return new Result(StatusCode.OK, true, "查询文章成功", pageResult);
    }

    /**
     * 根据频道ID获取文章列表
     */
    @PostMapping("/channel/{channelId}/{page}/{size}")
    public Result pageSearchByChannelId(@PathVariable("channelId") String channelId,
                                        @PathVariable("page") Integer current,
                                        @PathVariable("size") Integer size) {
        Page<Article> pageData = new Page<>(current, size);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("channelid", channelId);
        return getResultWithPage(pageData, wrapper);
    }

    /**
     * 点赞与取消点赞
     * 并发送通知或删除通知
     */
    @PutMapping("/thumbup/{articleId}")
    public Result thumbup(@PathVariable("articleId") String articleId) {
        //模拟获取用户id
        String userId = "1";
        boolean flag = articleService.thumbup(articleId, userId);
        if (flag) {
            return new Result(StatusCode.OK, true, "文章点赞成功", null);
        } else {
            return new Result(StatusCode.OK, true, "文章取消点赞成功", null);
        }

    }

    /**
     * 文章分页
     */
    @PostMapping("/search/{page}/{size}")
    public Result searchByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody Article article) {
        Page<Article> pageData = new Page<>(page, size);
        QueryWrapper<Article> wrapper = new QueryWrapper<>(article);
        return getResultWithPage(pageData, wrapper);

    }

    /**
     * 根据条件查询文章列表
     */
    @PostMapping("/search")
    public Result search(@RequestBody Map<String, Object> map) {
        List<Article> articles = articleService.listByMap(map);
        return new Result(StatusCode.OK, true, "查询成功", articles);
    }

    /**
     * 根据ID删除文章
     */
    @DeleteMapping("/{articleId}")
    public Result deleteById(@PathVariable("articleId") String articleId) {
        articleService.removeById(articleId);
        return new Result(StatusCode.OK, true, "删除文章成功", null);
    }

    /**
     * 修改文章
     */
    @PutMapping("/{articleId}")
    public Result updateById(@PathVariable("articleId") String articleId, @RequestBody Article article) {
        article.setId(articleId);
        articleService.updateById(article);
        return new Result(StatusCode.OK, true, "修改文章成功", null);
    }

    /**
     * 根据ID查询文章
     */
    @GetMapping("/{articleId}")
    public Result findById(@PathVariable("articleId") String articleId) {
        Article article = articleService.getById(articleId);
        return new Result(StatusCode.OK, true, "根据ID查询文章成功", article);
    }

    /**
     * 新增文章,并推送给订阅者
     */
    @PostMapping()
    public Result save(@RequestBody Article article) {
        articleService.add(article);
        return new Result(StatusCode.OK, true, "增加文章成功", null);
    }

    /**
     * 文章全部列表
     */
    @GetMapping()
    public Result findAll() {
        List<Article> articleList = articleService.list();
        return new Result(StatusCode.OK, true, "查询文章列表成功", articleList);
    }

    /**
     * 订阅或者取消订阅文章作者
     */
    @PostMapping("/subscribe")
    public Result subscribe(@RequestBody Map<String, String> map) {
        //订阅成功返回true,取消订阅返回false
        Boolean flag = articleService.subscribe(map.get("articleId"), map.get("userId"));
        if (flag) {
            return new Result(StatusCode.OK,true, "订阅成功",null);
        } else {
            return new Result(StatusCode.OK,true, "取消订阅成功",null);
        }
    }

}
